<template>
  <div class="app-container">
    <!-- *************************************搜索区************************************* -->
    <!-- 分类 -->
    <PageSearch :showSearch.sync="showSearch" :getList="getList" :handleQuery="handleQuery" :resetQuery="resetQuery">
      <template #pagination>
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </template>
      <template #form>
        <el-form :model="queryParams" v-show="showSearch" ref="queryForm" size="small" :inline="true">
          <el-row>
            <!-- 组合名称 -->
            <el-form-item prop="combinationName">
              <el-input
                :placeholder="$t('组合名称')"
                v-model="queryParams.combinationName"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <!-- 卖家名称 -->
            <el-form-item prop="distrName">
              <el-input
                :placeholder="$t('卖家名称')"
                v-model="queryParams.distrName"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="combTemp">
              <el-input
                :placeholder="$t('组合模板名称')"
                v-model="queryParams.combTemp"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item prop="combTempSku">
              <el-input
                :placeholder="$t('组合sku名称')"
                v-model="queryParams.combTempSku"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-row>
        </el-form>
      </template>
      <template #actions>
        <!-- 增删改查 -->
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['distribution:distributorShopCombination:add']"
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >
              {{ $t('新增') }}
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              size="mini"
              icon="el-icon-upload2"
              @click="showImport = true"
              v-hasPermi="['distribution:skuShop:importSellerCombination']"
            >
              {{ $t('导入') }}
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['distribution:skuShop:exportSellerCombination']"
            >
              {{ $t('导出') }}
            </el-button>
          </el-col>
        </el-row>
      </template>
    </PageSearch>
    <el-table
      @row-click="(row) => $rowClick(row, clickedRows)"
      :row-class-name="({ row }) => $tableRowClassName(row, clickedRows)"
      :height="calTableHeight()"
      v-loading="loading"
      :data="tableData"
      ref="tableRef"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column type="expand" width="55">
        <template slot-scope="props">
          <div style="display: flex">
            <div style="width: 40px; display: flex; align-items: center; justify-content: center">
              <i class="el-icon-caret-right" style="font-size: 25px"></i>
            </div>
            <div style="flex: 1">
              <el-table ref="TableRef" :data="props.row.children">
                <el-table-column align="center" show-overflow-tooltip prop="skuNo" :label="$t('sku编号')">
                  <template slot-scope="scope">
                    <div @click="handleEdits(scope.row)" style="cursor: pointer; color: #1890ff">
                      {{ scope.row.skuNo }}
                    </div>
                  </template>
                </el-table-column>
                <el-table-column prop="imgUrl" :label="$t('图片')" align="center">
                  <template slot-scope="{ row }">
                    <el-image
                      fit="contain"
                      style="width: 50px; height: 50px"
                      :key="Math.random()"
                      v-hover="row.imgUrl"
                      class="goods-img-mini"
                      :src="row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
                    />
                  </template>
                </el-table-column>
                <!-- 名字 -->
                <el-table-column
                  show-overflow-tooltip
                  prop="skuNameCn"
                  :label="$t('中文名')"
                  align="center"
                  width="200"
                />
                <el-table-column
                  show-overflow-tooltip
                  prop="skuNameJp"
                  :label="$t('日文名')"
                  align="center"
                  width="200"
                />

                <!-- 组合数量 -->
                <el-table-column prop="combinationNum" :label="$t('组合数量')" width="120" align="center">
                  <template slot-scope="scope">
                    <el-input-number
                      :precision="0"
                      style="width: 100px"
                      v-model="scope.row.combinationNum"
                      controls-position="right"
                      :min="1"
                    />
                  </template>
                </el-table-column>
                <!-- 规格 -->
                <el-table-column
                  show-overflow-tooltip
                  prop="specificatio"
                  :label="$t('规格')"
                  align="center"
                  width="150"
                />

                <!-- 可售数量 -->
                <el-table-column show-overflow-tooltip prop="saleNum" :label="$t('可售数量')" align="center" />

                <!-- 操作 -->
                <el-table-column fixed="right" :label="$t('操作')" width="200" align="center">
                  <template slot-scope="{ row }">
                    <el-button
                      type="text"
                      @click="editNumber(row)"
                      v-hasPermi="['distribution:distributorShopCombination:edit']"
                    >
                      {{ $t('编辑数量') }}
                    </el-button>
                    <el-button
                      type="text"
                      @click="deleteCombinSku(row, props.row)"
                      v-hasPermi="['distribution:distributorShopCombination:remove']"
                    >
                      {{ $t('删除') }}
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </template>
      </el-table-column>
      <!-- 组合名称 -->
      <el-table-column show-overflow-tooltip prop="combinationName" :label="$t('组合名称')" align="center" width="200">
        <template #header="{ column }">
          <div class="sort-item">
            <div @click="resetSortQuery(column)">
              {{ column.label }}
            </div>
            <div class="sort-item-icon">
              <i
                class="el-icon-caret-top"
                @click="setSortQuery(column, 'asc')"
                :style="setSortIconStyle(column, 'asc')"
              />
              <i
                @click="setSortQuery(column, 'desc')"
                :style="setSortIconStyle(column, 'desc')"
                class="el-icon-caret-bottom"
              />
            </div>
          </div>
        </template>
        <template slot-scope="{ row }">
          <span style="color: #1890ff; cursor: pointer" @click="handleCompositeTemplate(row)">
            {{ row.combinationName }}
          </span>
        </template>
      </el-table-column>
      <!-- 卖家名称 -->
      <el-table-column show-overflow-tooltip prop="distrName" :label="$t('卖家名称')" align="center" />
      <!-- 图片 -->
      <el-table-column prop="combinationNameUrl" :label="$t('图片')" align="center" width="100">
        <template slot-scope="{ row }">
          <el-image
            fit="contain"
            style="width: 50px; height: 50px"
            :key="Math.random()"
            v-hover="row.combinationUrl"
            :src="row.combinationUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
          />
        </template>
      </el-table-column>
      <!-- 店铺关联数量 -->
      <el-table-column show-overflow-tooltip prop="shopNum" :label="$t('店铺关联数量')" align="center" />
      <!-- 可售数量 -->
      <el-table-column show-overflow-tooltip prop="saleNum" :label="$t('可售数量')" align="center" />
      <!-- 操作 -->
      <el-table-column :label="$t('操作')" align="center" fixed="right" width="150">
        <!-- <template slot="header" slot-scope="scope">
          <div style="margin-right: 40px">{{ $t('操作') }}</div>
        </template> -->
        <template slot-scope="{ row }">
          <el-dropdown @command="(command) => handleCommand(command, row)">
            <span class="el-dropdown-link"> 更多<i class="el-icon-arrow-down el-icon--right"></i> </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleAppend">
                {{ $t('追加') }}
              </el-dropdown-item>
              <el-dropdown-item command="handleEdit" v-hasPermi="['distribution:distributorShopCombination:edit']">
                {{ $t('编辑') }}
              </el-dropdown-item>
              <el-dropdown-item command="handleDelete" v-hasPermi="['distribution:distributorShopCombination:remove']">
                {{ $t('删除') }}
              </el-dropdown-item>
              <el-dropdown-item command="handleApplyTemplate" v-hasPermi="['distribution:skuTempProxyApply:addCom']">
                {{ $t('申请模板代做') }}
              </el-dropdown-item>
              <el-dropdown-item command="productionApply" v-hasPermi="['distribution:skuSellerApplyProduce:add']">
                {{ $t('出品申请') }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <el-button type="text" @click="handleAppend(row)">
            {{ $t('追加') }}
          </el-button>
          <el-button
            type="text"
            @click="handleEdit(row)"
            v-hasPermi="['distribution:distributorShopCombination:edit']">
            {{ $t('编辑') }}
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(row.id)"
            v-hasPermi="['distribution:distributorShopCombination:remove']">
            {{ $t('删除') }}
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageTotal > 0"
      :total="pageTotal"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 追加 -->
    <el-dialog
      :visible.sync="appendInfo.show"
      :title="$t('追加')"
      width="1000px"
      destroy-on-close
      :before-close="cancelAppend"
      :close-on-click-modal="false"
    >
      <el-form :model="appendInfo.queryParams" ref="queryForm" size="small" :inline="true" label-width="90px">
        <el-form-item :label="$t('sku编号')" prop="skuNo">
          <el-input
            v-model="appendInfo.queryParams.skuNo"
            :placeholder="$t('请输入')"
            clearable
            style="width: 240px"
            @keyup.enter.native="getAppendList"
          />
        </el-form-item>
        <el-form-item :label="$t('供应商名称')" prop="distrName">
          <el-input
            v-model="appendInfo.queryParams.distrName"
            :placeholder="$t('请输入')"
            clearable
            style="width: 240px"
            @keyup.enter.native="getAppendList"
          />
        </el-form-item>
        <el-form-item :label="$t('中文名')" prop="skuNameCn">
          <el-input
            v-model="appendInfo.queryParams.skuNameCn"
            :placeholder="$t('请输入')"
            clearable
            style="width: 240px"
            @keyup.enter.native="getAppendList"
          />
        </el-form-item>
        <el-form-item :label="$t('日文名')" prop="skuNameJp">
          <el-input
            v-model="appendInfo.queryParams.skuNameJp"
            :placeholder="$t('请输入')"
            clearable
            style="width: 240px"
            @keyup.enter.native="getAppendList"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getAppendList">
            {{ $t('搜索') }}
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        :height="calTableHeight() - 160"
        :data="appendInfo.tableData"
        ref="multipleTable"
        @selection-change="handleAppendSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <!-- sku -->
        <el-table-column align="center" show-overflow-tooltip prop="skuNo" :label="$t('sku编号')">
          <template slot-scope="scope">
            <div @click="handleEdits(scope.row)" style="cursor: pointer; color: #1890ff">
              {{ scope.row.skuNo }}
            </div>
          </template>
        </el-table-column>
        <!-- 图片 -->
        <el-table-column prop="imgUrl" :label="$t('图片')" align="center">
          <template slot-scope="scope">
            <el-image
              fit="contain"
              :key="Math.random()"
              v-hover="scope.row.imgUrl"
              style="width: 50px; height: 50px"
              :src="scope.row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
            />
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="skuNameCn" width="200" :label="$t('中文名')" align="center" />
        <el-table-column show-overflow-tooltip prop="skuNameJp" width="200" :label="$t('日文名')" align="center" />

        <el-table-column prop="combinationNum" :label="$t('组合数量')" align="center">
          <template slot-scope="scope">
            <el-input-number
              :precision="0"
              style="width: 100px"
              v-model="scope.row.combinationNum"
              controls-position="right"
              :min="1"
            />
          </template>
        </el-table-column>
        <el-table-column fixed="right" :label="$t('操作')" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="confirmAppend(scope.row)">
              {{ $t('确定') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="appendInfo.pageTotal > 0"
        :total="appendInfo.pageTotal"
        :page.sync="appendInfo.queryParams.pageNum"
        :limit.sync="appendInfo.queryParams.pageSize"
        @pagination="getAppendList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancelAppend">
          {{ $t('关闭') }}
        </el-button>
      </div>
    </el-dialog>
    <!-- sku详情/修改 -->
    <Skucontmoduler @relaod="relaod" ref="Skucontmoduler"></Skucontmoduler>
    <Import
      action="/distribution/skuShop/importSellerCombination"
      templateUrl="distribution/distributorShopCombination/tempDownload"
      @success="getList"
      :show.sync="showImport"
    />

    <!-- 组合模板 -->
    <CompositeTemplate
      :formData="compositeTemplate.formData"
      :show.sync="compositeTemplate.show"
      :combinationId="compositeTemplate.formData.id"
      @success="getList"
    />

    <!-- 新增修改 -->
    <AddAndEdit
      :show.sync="addAndEdit.show"
      @getList="getList"
      :formData="addAndEdit.formData"
      :type="addAndEdit.type"
    />
    <!-- 添加文案代做 -->
    <Wenan :show.sync="wenan.show" @getList="getList" :formData="wenan.formData" />
    <!-- 出品申请 -->
    <ProductionApply :show.sync="productionApply.show" @getList="getList" :formData="productionApply.formData" />
  </div>
</template>

<script>
import CompositeTemplate from '@/components/CompositeTemplate'
import ProductionApply from './components/productionApply.vue'
import Wenan from './components/wenan.vue'

import {
  bindCombinationSku,
  deleteDistributorShopCombination,
  deleteDistributorShopCombinationSku,
  getCombinationSkuListById,
  getDistributorShopCombinationList,
  insertDistributorShopCombination,
  updateCombinationSkuNum,
  updatesDistributorShopCombination,
} from '@/api/distribution/sku/assemblyList'
import ImageUpload from '@/components/ImageUpload'
import Import from '@/components/Import/index.vue'
import { default as AddAndEdit, default as SkuList } from './components/addAndEdit.vue'

export default {
  activated() {
    this.$nextTick(() => this.$refs['tableRef']?.doLayout())
  },
  name: 'AssemblyList',
  components: {
    ImageUpload,
    Import,
    CompositeTemplate,
    AddAndEdit,
    SkuList,
    Wenan,
    ProductionApply,
  },
  data() {
    return {
      /**添加文案代做 */
      wenan: {
        show: false,
        formData: {},
      },
      /**出品申请 */
      productionApply: {
        show: false,
        formData: {},
      },
      /**新增修改 */
      addAndEdit: {
        show: false,
        type: '',
        formData: {},
      },
      /**点击的表格项 */
      clickedRows: [],
      /**组合模板 */
      compositeTemplate: {
        show: false,
        formData: {},
      },
      showSearch: false,
      /**
       * 导入组件的显示与隐藏
       */
      showImport: false,

      showDialog: false,
      /**追加 */
      appendInfo: {
        tableData: [],
        tableCheck: [],
        show: false,
        form: {},
        /**数据总条数 */
        pageTotal: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
      },
      showName: '',
      tableData: [],
      /**数据总条数 */
      pageTotal: 0,
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
  },
  watch: {
    'queryParams.pageNum': function (val) {
      this.clickedRows = []
    },
  },
  created() {
    this.getList()
  },
  methods: {
    handleCommand(command, row) {
      switch (command) {
        case 'handleAppend' /**追加 */:
          this.handleAppend(row)
          break
        case 'handleEdit' /**编辑 */:
          this.handleEdit(row)
          break
        case 'handleDelete' /**删除 */:
          this.handleDelete(row.id)
          break
        case 'handleApplyTemplate' /**申请模板代做 */:
          this.handleApplyTemplate(row)
          break

        case 'productionApply' /**文案代做 */:
          this.productionApply.formData = row
          this.productionApply.show = true
          break

        default:
          break
      }
    },
    handleApplyTemplate(row) {
      this.wenan.formData = row
      this.wenan.show = true
    },
    handleCompositeTemplate(row) {
      this.compositeTemplate.formData = row
      this.compositeTemplate.show = true
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'distribution/skuShop/exportSellerCombination',
        {
          ...this.queryParams,
        },
        `exportSellerCombination_${new Date().getTime()}.xlsx`,
      )
    },
    relaod() {
      this.getList()
    },
    handleEdits(row) {
      if (!row.skuId) {
        row.skuId = row.id
      }
      this.$refs.Skucontmoduler.setSkuInfo(row)
      this.$refs.Skucontmoduler.checkNumber('basicInformationSeller')
    },
    cancelAppend() {
      this.appendInfo.show = false
    },
    editNumber(row) {
      console.log(` ==>`, JSON.parse(JSON.stringify(row)))
      const _row = JSON.parse(JSON.stringify(row))
      const data = {
        id: _row.id,
        combinationNum: _row.combinationNum,
      }
      updateCombinationSkuNum(data).then((res) => {
        if (res.code == 200) {
          this.$message({ type: 'success', message: res.msg })
          this.getList()
        }
      })
    },
    deleteCombinSku(row, father) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        deleteDistributorShopCombinationSku(row.id).then((res) => {
          if (res.code == 200) {
            this.$message({ type: 'success', message: res.msg })
            this.getList()
          }
        })
      })
    },
    handleAppendSelectionChange(rows) {
      this.appendInfo.tableCheck = rows
    },
    getAppendList() {
      getCombinationSkuListById(this.appendInfo.queryParams).then((res) => {
        this.appendInfo.tableData = res.rows || []
        this.appendInfo.pageTotal = res.total || 0
      })
    },
    handleAppend(row) {
      this.appendInfo.show = true
      this.appendInfo.form = JSON.parse(JSON.stringify(row))
      this.appendInfo.queryParams.combinationId = row.id
      this.getAppendList()
    },
    confirmAppend(row) {
      if (!row.combinationNum) {
        this.$message.warning(this.$t('请输入') + '!')
        return
      }
      const data = {
        combinationId: this.appendInfo.form.id,
        skuId: row.id,
        skuNo: row.skuNo,
        combinationNum: row.combinationNum,
      }
      bindCombinationSku([data]).then((res) => {
        if (res.code == 200) {
          this.$message({ type: 'success', message: res.msg })

          this.getList()
          this.getAppendList()
        }
      })
    },

    handleAdd() {
      this.addAndEdit.type = 'add'
      this.addAndEdit.formData = {}
      this.addAndEdit.show = true
    },
    handleEdit(row) {
      this.addAndEdit.type = 'edit'
      this.addAndEdit.formData = JSON.parse(JSON.stringify(row))
      this.addAndEdit.show = true
    },

    handleDelete(id) {
      this.$confirm(this.$t('确认删除?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        deleteDistributorShopCombination(id).then((res) => {
          if (res.code == 200) {
            this.$message({ type: 'success', message: res.msg })

            this.showDialog = false
            this.getList()
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },

    // 取消申请
    cancel() {
      this.editDialogConfig = !this.editDialogConfig
    },
    submitForm() {
      this.$refs.elForm.validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.form))
          // 新增
          if (this.showName == 'add') {
            // if (!this.form.combinationUrl) {
            //   this.$message.info("请上传图片");
            //   return;
            // }
            insertDistributorShopCombination(data).then((res) => {
              if (res.code == 200) {
                this.$message({ type: 'success', message: res.msg })

                this.showDialog = false
                this.getList()
              }
            })
          }
          // 修改
          if (this.showName == 'edit') {
            updatesDistributorShopCombination(data).then((res) => {
              if (res.code == 200) {
                this.$message({ type: 'success', message: res.msg })

                this.showDialog = false
                this.getList()
              }
            })
          }
        }
      })
    },
    // 展示申请
    submitFormData(data) {
      this.curentData = data
      this.editDialogConfig = !this.editDialogConfig
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true
      getDistributorShopCombinationList(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.pageTotal = res.total
        this.loading = false
        this.$nextTick(() => this.$refs['tableRef']?.doLayout())
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.value1 = null
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = null
      })
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10

      this.getList()
    },

    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false
      this.isExpandAll = !this.isExpandAll
      this.$nextTick(() => {
        this.refreshTable = true
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.goods {
  &-img {
    &-box {
      position: relative;
    }

    &-mini {
      height: 50px;
      border-radius: 3px;

      &:hover {
        & + .goods-img-big {
          display: block;
        }
      }
    }

    &-big {
      // display: none;
      position: absolute;
      z-index: 100;
      top: -180px;
      left: 0;
      transform: translate(0, 70px);
      width: 200px;
      height: 200px;
      border-radius: 3px;
    }
  }
}

.bigImg {
  border-radius: 3px;
  z-index: 10;
  position: fixed;
  width: 200px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
